package dormitory;

import java.io.InputStream;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;

import javax.activation.DataSource;

import org.rex.DB;
import org.rex.RMap;
import org.rex.db.exception.DBException;
import com.mysql.jdbc.Connection;


public class Administrator {
	private String username;
	private String password;
	private String name;
	private String sex;
	private int age;
	private Date createTime;
	public Administrator(){
		
	}
	public Administrator(String username,String password,String name,String sex,int age,Date time){
		this.username = username;
		this.password = password;
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.createTime=time;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public boolean login(String username,String password) throws DBException{
		
		if(username==null||password==null){
			return false;
		}
		String sql = "SELECT * FROM administrator where username='"+username+"'and password='"+password+"'";
		 java.util.List<RMap> list = DB.getMapList(sql);
		 if(list==null){
			 return false;
		 }else {
			 System.out.println(list);
				return true;
		 }
	}
	public void create(Administrator ad) throws DBException{
		boolean add=false;
		Scanner in=new Scanner(System.in);
		
		ad.username=in.next();
		ad.password=in.next();
		ad.name=in.next();
		ad.sex=in.next();
		ad.age=in.nextInt();
		ad.createTime=new Date();
		if(ad.username==null||ad.password==null||ad.name==null||ad.sex==null||ad.age>=120||ad.age<0){
			System.out.println("请输入正确的信息");
		}else	{
			 String sql = "INSERT INTO administrator(username, password, name, sex, age, create_time) VALUES (#{username},#{password},#{name},#{sex},#{age},#{createTime})";
			 System.out.println(ad.password+ad.username+ad.name+ad.sex+ad.age+ad.createTime);
			 int i = DB.update(sql, ad);
		     System.out.println( i + " row inserted.");
		}
	}
	public void delete() throws DBException{
		Scanner in=new Scanner(System.in);
		String sql = "delete from REX_TEST where ID=#{id} and NAME=#{name}";
    	Map prameters = new HashMap();
    	prameters.put("id", in.nextInt());
    	prameters.put("name", in.next());
    	int i =0;
    	i=DB.update(sql, prameters);
    	if(i==0){
    		System.out.println("fail.");
    	}else {
    		System.out.println("succes.");
    	}
	}
	public void retrieve(Administrator ad) throws DBException{
		String sql = "SELECT * FROM administrator WHERE username = #{username} and password = #{password}";
    	Scanner in=new Scanner(System.in);
    	ad.setUsername(in.next());
    	ad.setPassword(in.next());
    	ad = DB.get(sql, ad, Administrator.class);
    	if(ad==null){
    		System.out.println("not exit");
    	}else {
    		System.out.println(ad.name +ad.sex +ad.age);
    	}
	}
	public static void main(String[] args) throws DBException {
		// TODO Auto-generated method stub
//		String sql = "CREATE TABLE REX_TEST (ID int(11) NOT NULL, NAME varchar(30) NOT NULL, CREATE_TIME time NOT NULL)";
//        DB.update(sql);
//        System.out.println("table created.");
//		String sql1 = "INSERT INTO REX_TEST(ID, NAME, CREATE_TIME) VALUES (?, ?, ?)";
//        int i = DB.update(sql1, new Object[]{1, "test", new Date()});
//        System.out.println( i + " row inserted.");
		Administrator ad=new Administrator();
		ad.retrieve(ad);
	}

}

